﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using SQLExpressDAL;

public partial class Manageweb_Coffee_BuyCoffee : System.Web.UI.Page
{
    public string StrKey;

    protected override void Render(HtmlTextWriter writer)
    {
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        base.Render(htw);

        if (!hidMiYao.Value.Equals("1"))
        {
            writer.Write(sw.ToString().Replace("<body", "<body onload=javascript:openLayer('test','test_con');"));
        }
        else
        {
            writer.Write(sw);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        StrKey = CheckData();
        if (hidMiYao.Value.Equals("1"))
        {
            BindCoffeeList();
            GetAccountInfo();
        }

        //if (!Page.IsPostBack)
        //{
        //    BindCoffeeList();
        //    GetAccountInfo();
        //}
    }


    //获取密钥Cdefine5
    public string CheckData()
    {
        SqlParameter[] paras ={ 
                new SqlParameter("@userid",SqlDbType.VarChar,30)
            };
        paras[0].Value = Session["userid"];

        StrKey = SqlHelper.FillDataSet(CommandType.StoredProcedure, "UP_GetUserKey", paras).Tables[0].Rows[0][1].ToString();
        return StrKey;
    }
    /// <summary>
    /// 初始化会员的总账户信息
    /// </summary>
    protected void InitAccountInfo()
    {
        SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter adp = new SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter();
        SQLExpressDAL.TransferRecords.UsersAccountRow row = (SQLExpressDAL.TransferRecords.UsersAccountRow)adp.GetSingleAccount(Session["UserID"].ToString()).Rows[0];

        SQLExpressDAL.Users.UsersDataTable tab = (SQLExpressDAL.Users.UsersDataTable)Session["UserInfo"];


        Label lblAB1 = Master.FindControl("lblAB1") as Label;

        Label lblAB2 = Master.FindControl("lblAB2") as Label;

        Label lblAB3 = Master.FindControl("lblAB3") as Label;

        Label lblAB4 = Master.FindControl("lblAB4") as Label;

        lblAB1.Text = row.AB1.ToString("f2");
        lblAB2.Text = row.AB2.ToString("f2");
        lblAB3.Text = row.AB3.ToString("f2");
        lblAB4.Text = row.AB4.ToString("f2");


    }


    private void BindCoffeeList()
    {

        SQLExpressDAL.CoffeeTableAdapters.RecordTableAdapter adp = new SQLExpressDAL.CoffeeTableAdapters.RecordTableAdapter();
        SQLExpressDAL.Coffee.RecordDataTable tab = adp.GetBuyCoffeeRecord(Session["UserID"].ToString());

        this.GV_CoffeeList.DataSource = tab;
        this.GV_CoffeeList.DataBind();
    }


    protected void btnBuy_Command(object sender, CommandEventArgs e)
    {
        String[] coffeeInfo = e.CommandArgument.ToString().Split(new char[] { '|' });

        this.txtCoffeeName.Text = coffeeInfo[0];

        this.txtCoffeePrice.Text = coffeeInfo[2];
        this.hidCoffeeID.Value = coffeeInfo[3];
        this.hidCoffeeCount.Value = coffeeInfo[1];

        this.TABLE_Buy.Visible = true;
        this.LB_ManageUser.Text = "";
    }


    protected void Btn_Submit_Click(object sender, EventArgs e)
    {
        string msg = "购买成功";
        try
        {
            string userID = Session["UserID"].ToString();
            string recordID = this.hidCoffeeID.Value;
            string coffeeName = this.txtCoffeeName.Text.Trim();
            decimal coffeePrice = Convert.ToDecimal(this.txtCoffeePrice.Text.Trim());


            int coffeeCount = Convert.ToInt16(this.txtCoffeeCount.Text.Trim());
            int currCount = Convert.ToInt16(this.hidCoffeeCount.Value);
            decimal costMoney = coffeeCount * coffeePrice;
            decimal vCurrency = Convert.ToDecimal(ViewState["VCurrency"]);
            if (coffeeCount > currCount)
            {
                msg = "购买数量超过现有数量";


            }
            else if (costMoney > vCurrency)
            {
                msg = "账户金额不足";

            }
            else
            {
                SQLExpressDAL.StockTableAdapters.RecordTableAdapter adp = new SQLExpressDAL.StockTableAdapters.RecordTableAdapter();
                int flag = adp.InsertBuyRecord("admin", recordID, coffeeName, coffeePrice, coffeeCount, "CoffeeBuy", Convert.ToDecimal(0.0), userID, -1);

                if (flag < 1)
                {
                    msg = "购买失败";
                }
            }


        }
        catch (Exception ex)
        {
            msg = "购买失败";
        }
        finally
        {

            this.LB_ManageUser.Text = msg;
            this.TABLE_Buy.Visible = false;
            GetAccountInfo();
            BindCoffeeList();
            ClearTxt();

        }
    }


    public void GetAccountInfo()
    {
        SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
        SQLExpressDAL.Users.UsersDataTable tab = userAdp.GetSingleUserInfo(Session["UserID"].ToString());

        DataRow dr = tab.Rows[0];


        Label lblUserName = Master.FindControl("lblUserName") as Label;

        Label lblAccount = Master.FindControl("lblAccount") as Label;


        SQLExpressDAL.DealRecord obj = new SQLExpressDAL.DealRecord();
        Hashtable htab = obj.GetMemAccountMoney(Session["UserID"].ToString());

        if (htab["IsSuccess"].ToString().Equals("1"))
        {
            //Label lblAccountMoney = Master.FindControl("lblAccountMoney") as Label;

            //lblAccountMoney.Text = htab["Count"].ToString();

        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('金额统计错误');", true);
            return;
        }


        Label LjScore = Master.FindControl("lblLjScore") as Label;
        LjScore.Text = dr["ljscore"].ToString();
        Label SpecialScore = Master.FindControl("lblSpecialScore") as Label;
        SpecialScore.Text = dr["specialscore"].ToString();

    }

    private void ClearTxt()
    {
        this.txtCoffeeCount.Text = "";
        this.txtCoffeeName.Text = "";
        this.txtCoffeePrice.Text = "";

        this.hidCoffeeCount.Value = "";
        this.hidCoffeeID.Value = "";
    }


    protected void GV_CoffeeList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GV_CoffeeList.PageIndex = e.NewPageIndex;
        BindCoffeeList();
    }
}
